package com.ly.controller;

import com.ly.bean.Admin;
import com.ly.bean.Menu;
import com.ly.bean.Result;
import com.ly.bean.Roles;
import com.ly.service.AdminService;
import com.ly.service.MenuService;
import com.ly.service.RolesService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李岩
 * @date 2021/12/27-14:21
 * @describe
 */
@Controller
@RequestMapping("/role/")
public class RoleController {
    private final Logger logger= LoggerFactory.getLogger(RoleController.class);

    @Reference(url = "dubbo://localhost:20886")
    private RolesService rolesService;
    @Reference(url = "dubbo://localhost:20886")
    private MenuService menuService;
    @Reference(url = "dubbo://localhost:20886")
    private AdminService adminService;

    private RoleController(){
        System.out.println("进入了 RoleController  controller层");
    }

    @RequestMapping("manager")
    public String manager(Model model){

        try {
            logger.info("角色管理");
            //1.加载所有的角色列表
            List<Roles> rolesList = rolesService.selectAllRole();
            //2.加载所有的一级菜单和二级菜单
            Map<String, Object> map = new HashMap<>();
            List<Menu> firstMenuList = menuService.findMenuByMap(map);
            Map<Integer, Object> secondMenuMap = new HashMap<>();
            for (Menu menu : firstMenuList) {
                map.put("parentId",menu.getId());
                List<Menu> list = menuService.findMenuByMap(map);
                secondMenuMap.put(menu.getId(),list);
            }
            model.addAttribute("rolesList",rolesList);
            model.addAttribute("firstMenuList",firstMenuList);
            model.addAttribute("secondMenuMap",secondMenuMap);
            return "role_manager.html";
            //3.获取当前角色拥有的权限列表

        } catch (Exception e) {
            logger.error("角色管理 出现异常  "+e);
        }
        return "error";
    }
    //根据角色id 查询所有菜单
    @RequestMapping("findRoleMenuByRid")
    @ResponseBody
    public Result findRoleMenuByRid(Integer roleId){
        logger.info("根据角色id 查询所有菜单 "+roleId);
        try {
            List<Menu> menuList = menuService.findAllMenuByRid(roleId);
            return Result.getResult(true,200,"查询成功",menuList);
        } catch (Exception e) {
            logger.error("根据角色id 查询所有菜单 出现异常  "+e);
        }
        return Result.getResult(false,444,"查询失败",null);
    }
    //更改角色和菜单的关系
    @RequestMapping("updateRoleMenu")
    public String updateRoleMenu(Integer roleId,@RequestParam("menuId") List<Integer> menuIds){
        logger.info("更改角色和菜单的关系 "+roleId);

        try {
            //先删除角色的菜单表
            rolesService.deleteRolesMenuByRid(roleId);
            //然后在一条条的添加进去
            for (Integer menuId : menuIds) {
                rolesService.addRolesMenu(roleId,menuId);
            }
            return "redirect:/role/manager";
        } catch (Exception e) {
            logger.error("更改角色和菜单的关系 出现异常 "+e);
        }
        return "error";
    }

    //更改用户和角色的关系
    //展示用户管理页面
    @RequestMapping("adminManager")
    public String userManager(Model model){
        try {
            logger.info("展示用户管理页面 ");
            //获取所有admin信息
            List<Admin> adminList = adminService.selectAllAdmin();
            //获取所有的角色对象
            List<Roles> rolesList = rolesService.selectAllRole();

            model.addAttribute("adminList",adminList);
            model.addAttribute("rolesList",rolesList);
            return "admin_manager";
        } catch (Exception e) {
            logger.error("更改角色和菜单的关系 出现异常 "+e);
        }

        return "error";
    }

    //根据aId 查询角色
    @RequestMapping("findRoleByaId")
    @ResponseBody
    public Result findRoleByaId(Integer aId){
        logger.info("根据角色id 查询所有菜单 "+aId);
        try {
            List<Roles> rolesList = rolesService.selectRoleByaId(aId);
            return Result.getResult(true,200,"查询成功",rolesList);
        } catch (Exception e) {
            logger.error("根据角色id 查询所有菜单 出现异常  "+e);
        }
        return Result.getResult(false,444,"查询失败",null);
    }

    //更改admin 和角色之间的关系
    @RequestMapping("updateRoleByaId")
    public String updateRoleByaId(Integer aId,@RequestParam("roleId") List<Integer> roleIds){
        logger.info("更改admin 和角色之间的关系 "+aId);
        try {
            adminService.updateRoleByaId(aId,roleIds);
            return "redirect:/role/adminManager";
        } catch (Exception e) {
            logger.error("更改admin 和角色之间的关系 出现异常 "+e);
        }
        return "error";
    }
    //跳转到 展示所有角色的页面
    @RequestMapping("showAllRole")
    public String showAllRole(Model model){

        try {
            logger.info("跳转到 展示所有角色的页面");
            //获取所有的角色对象
            List<Roles> rolesList = rolesService.selectAllRole();
            model.addAttribute("rolesList",rolesList);
        } catch (Exception e) {
            logger.error("跳转到 展示所有角色的页面 出现异常 "+e);
        }
        return "role_list";
    }
    //跳转到添加角色页面
    @RequestMapping("toAddRole")
    public String toAddRole(){

        try {
            logger.info("跳转到添加角色页面 ");
            return "role_add";
        } catch (Exception e) {
            logger.error("跳转到添加角色页面 "+e);
        }
        return "role_list";
    }
    //添加角色
    @RequestMapping("addRole")
    public String addRole(Roles roles){
        try {
            logger.info("添加角色");
            rolesService.insertRoleSelective(roles);
            return "redirect:/role/showAllRole";
        } catch (Exception e) {
            logger.error("添加角色 出现异常 "+e);
        }
        return "error";
    }
    //跳转到修改角色页面
    @RequestMapping("toUpdateRole")
    public String toUpdateRole(Model model,Integer id){

        try {
            logger.info("跳转到修改角色页面 "+id);
            //根据角色id获取角色对象信息
            Roles role = rolesService.selectRoleByPrimaryKey(id);
            model.addAttribute("role",role);
            return "role_update";
        } catch (Exception e) {
            logger.error("跳转到修改角色页面 "+e);
        }
        return "role_list";
    }
    //修改角色信息
    @RequestMapping("updateRole")
    public String updateRole(Roles roles){
        try {
            logger.info("修改角色信息 "+roles);
            rolesService.updateRoleByPrimaryKeySelective(roles);
            return "redirect:/role/showAllRole";
        } catch (Exception e) {
            logger.error("修改角色信息 出现异常 "+e);
        }
        return "error";
    }
    //删除角色信息
    @RequestMapping("deleteRole")
    public String deleteRole(Integer id){
        try {
            logger.info("删除角色信息 "+id);
            //因为存在外键关联  所哟要删除角色  需要先根据角色id 删除 roles_admins,roles_menu  调整到了业务层
            /*rolesService.deleteRoleAdminByPrimaryKey(id);
            rolesService.deleteRoleMenuByPrimaryKey(id);*/
            rolesService.deleteRoleByPrimaryKey(id);
            return "redirect:/role/showAllRole";
        } catch (Exception e) {
            logger.error("删除角色信息 出现异常 "+e);
        }
        return "error";
    }

}
